package com.talkweb.ncfw.security.springsecurity;

import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;

import com.talkweb.ncfw.data.DataMap;
import com.talkweb.ncfw.data.IDataMap;
import com.talkweb.ncfw.data.IUserSession;
import com.talkweb.ncfw.utils.AssertUtils;
import com.talkweb.ncfw.utils.reflection.ReflectionUtils;

/**
 * <p>文件名称: UserSessionImpl.java</p>
 * <p>文件描述: 用户session信息</p>
 * <p>版权所有: 版权所有(C)2010</p>
 * <p>公　　司: 拓维信息系统股份有限公司</p>
 * <p>内容摘要: 简要描述本文件的内容，包括主要模块、函数及能的说明</p>
 * <p>其他说明: 其它内容的说明</p>
 * <p>完成日期: 2011-5-30</p>
 * <p>修改记录1:</p>
 * <pre>
 *    修改日期:
 *    修 改 人:
 *    修改内容:
 * </pre>
 * <p>修改记录2：…</p>
 * @author  Wuqingming
 */
public class UserSession extends User implements IUserSession {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2441920717000677247L;
	
	private String userid;		//用户ID
	private String[] roles;		//用户角色信息
	private String loginid;		//登录ID
	private String username;	//用户名
	private String email;		//邮件
	private String mobile;		//手机号码
	private String loginip;		//登录IP地址
	
	private IDataMap<String, Object> attributes = null;		//用于字段的扩展

	public UserSession(String username, String password,
			boolean enabled, boolean accountNonExpired,
			boolean credentialsNonExpired, boolean accountNonLocked,
			Collection<? extends GrantedAuthority> authorities) {
		super(username, password, enabled, accountNonExpired, credentialsNonExpired,
				accountNonLocked, authorities);
		this.loginid = username;
	}
	
	public void putAttribute(String propertyName, Object value) {
		AssertUtils.notEmpty(propertyName, "propertyName can not be empty.");
		Field field = ReflectionUtils.getAccessibleField(this, propertyName);
		if (field != null) {
			ReflectionUtils.invokeSetterMethod(this, propertyName, value);
		}
		put(propertyName, value);
	}

	public Object getAttribute(String propertyName) {
		AssertUtils.notEmpty(propertyName, "propertyName can not be empty.");
		Field field = ReflectionUtils.getAccessibleField(this, propertyName);
		if (field != null) {
			return ReflectionUtils.invokeGetterMethod(this, propertyName);
		}
		if (attributes == null) {
			return null;
		}
		return attributes.get(propertyName);
	}
	
	//是否包含某属性
	public boolean hasAttribute (String propertyName) {
		Field field = ReflectionUtils.getAccessibleField(this, propertyName);
		if (field != null) {
			return true;
		}
		if (attributes != null && attributes.containsKey(propertyName)) {
			return true;
		}
		return false;
	}

	protected void put (String key, Object value) {
		if (attributes == null) {
			attributes = new DataMap<String, Object>();
		}
		attributes.put(key, value);
	}

	public String getUserid() {
		return userid;
	}

	public String[] getRoles() {
		return this.roles;
	}

	public String getLoginid() {
		return loginid;
	}

	public String getUsername() {
		return username;
	}

	public String getEmail() {
		return this.email;
	}

	public String getMobile() {
		return this.mobile;
	}

	public String getLoginip() {
		return this.loginip;
	}

	@SuppressWarnings("unchecked")
	public void init(String userid, String[] roles, String loginid,
			String username, String email, String mobile, String loginip,
			IDataMap<String, Object> otherAttributes) {
		this.userid = userid;
		this.roles = roles;
		this.loginid = loginid;
		this.username = username;
		this.email = email;
		this.mobile = mobile;
		this.loginip = loginip;
		if (this.attributes == null) {
			this.attributes = otherAttributes;
		} else if (otherAttributes != null) {
			Iterator iter = otherAttributes.keySet().iterator();
			while (iter.hasNext()) {
				Map.Entry entry 	= (Map.Entry) iter.next();
				String key 			= entry.getKey().toString();
				Object value		= entry.getValue();
				this.put(key, value);
			}
			
		}
	}

	public void clear() {
		this.userid = null;
		this.roles = null;
		this.loginid = null;
		this.username = null;
		this.email = null;
		this.mobile = null;
		this.loginip = null;
		this.attributes = null;
	}
	
}

